package com.xiaoxin.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.mongodb.client.result.UpdateResult;
import com.xiaoxin.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author xiaoxiaode
 * @date 2021-09-10-14:47
 **/
@DubboService
public class UserLocationImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String address) {
        try {
            // 查询用户位置信息
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            // 不存在位置则保存
            if (location == null) {
                location = new UserLocation();
                location.setUserId(userId);
                location.setAddress(address);
                location.setCreated(System.currentTimeMillis());
                location.setLastUpdated(System.currentTimeMillis());
                location.setUpdated(System.currentTimeMillis());
                location.setLocation(new GeoJsonPoint(longitude, latitude));
                mongoTemplate.save(location);
            } else {
                // 存在则更新
                Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
                        .set("updated", System.currentTimeMillis())
                        .set("lastUpdated", location.getUpdated())
                        .set("address", address);
                UpdateResult result = mongoTemplate.updateFirst(query, update, UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public List<Long> queryNearUser(Long userId, Double metre) {
        // 查询当前用户的位置
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if (location == null) {
            return null;
        }
        // 绘制圆
        GeoJsonPoint point = location.getLocation();
        Distance distance = new Distance(metre / 1000, Metrics.KILOMETERS);
        Circle circle = new Circle(point, distance);

        // 查询
        Query query1 = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> locations = mongoTemplate.find(query1, UserLocation.class);
        List<Long> ids = CollUtil.getFieldValues(locations, "userId", Long.class);
        return ids;
    }
}
